package br.com.array.android.ma.controller;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.array.android.ma.R;
import br.com.array.android.ma.view.ItemMenuModel;

import java.util.List;

/**
 * Created by desenvolv04 on 20/05/2014.
 */
public class ItemMenuAdapter extends BaseAdapter{

    private List<ItemMenuModel> itens;
    private LayoutInflater inflater;

    public ItemMenuAdapter(Context context, List<ItemMenuModel> itens) {
        this.inflater = LayoutInflater.from(context);
        this.itens = itens;
    }

    @Override
    public int getCount() {
        return itens.size();
    }

    @Override
    public Object getItem(int position) {
        return itens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_menu, null);
            holder = new ViewHolder();

            holder.ivIcone = (ImageView) convertView.findViewById(R.id.iv_menu);
            holder.tvTitulo = (TextView) convertView.findViewById(R.id.value_titulo_menu);
            holder.tvDescricao = (TextView) convertView.findViewById(R.id.value_descricao_menu);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ItemMenuModel item = itens.get(position);
        holder.ivIcone.setImageResource(item.getIcone());
        holder.tvTitulo.setText(item.getTitulo());
        holder.tvDescricao.setText(item.getDescricao());

        return convertView;
    }

    static class ViewHolder {
        ImageView ivIcone;
        TextView tvTitulo;
        TextView tvDescricao;
    }
}
